# coding: utf-8

import json
import io
import random
from datetime import datetime

# 将python2的编码格式改为utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

def date_time(time_format):
    now = datetime.now()
    formatted_time = now.strftime(time_format)
    return formatted_time

response_head = {
    "accessToken": "2024021410000008eb15aa4c894fbd807dc25ddca9cba1-0019-1705284000",
    "busType": "BBSS",
    "cutOffDay": date_time('%Y%m%d'),
    "domain": "COP",
    "envFlag": "0",
    "routeType": "00",
    "routeValue": "230",
    "sessionID": str(random.randint(100000, 999999))+ str(random.randint(100000, 999999)) + date_time('%Y%m%d%H%M%S') + str(random.randint(100000, 999999)),
    "sign": "F052B3D2C0DFEB1B561C42E0E3C235FB",
    "signMethod": "md5",
    "timeStamp": date_time('%Y%m%d%H%M%S'),
    "transIDO": str(random.randint(100000, 999999))+ str(random.randint(100000, 999999)) + date_time('%Y%m%d%H%M%S') + str(random.randint(100000, 999999)),
    "userPartyID": "BBSS9980",
    "version": "1.0.0",
    "content": "",

    "ServiceType": "POOrderService"
}

orderinfo_add_dict = {
    "finishTime": date_time('%Y%m%d%H%M%S'),
    "notes": "",
    "busNeedDegree": "4",
    "poOrderRatePlans": [],
    # "productOfferingID": "32300207578",
    "timeStamp": date_time('%Y%m%d%H%M%S'),
    "poAudit": [
        {
            "auditTime": date_time('%Y%m%d%H%M%S'),
            "auditor": "订单创建",
            "auditDesc": "订单创建"
        },
        {
            "auditTime": date_time('%Y%m%d%H%M%S'),
            "auditor": "订单同步",
            "auditDesc": "订单同步成功"
        },
        {
            "auditTime": date_time('%Y%m%d%H%M%S'),
            "auditor": "成功归档",
            "auditDesc": "成功归档"
        }
    ],
    "poOrderCharge": []
    }





def read_json(file_path):
    with io.open(file_path, 'rt', encoding='utf-8') as f:
        json_data = json.load(f)

    return json_data

def update_node_value(json_data):
    productID = 62300000000 + random.randint(1000000, 9909999)
    productOfferingID = 32300000000 + random.randint(1000000, 9990999)
    poOrderNumber = date_time('%y%m%d') + str(random.randint(100000, 999999))
    json_data['orderInfo']['poOrderNumber'] = '230A'+str(poOrderNumber)
    print('poOrderNumber:', json_data['orderInfo']['poOrderNumber'])
    json_data['orderInfo']['productOfferingID'] = str(productOfferingID)
    productOrders_lis = json_data['orderInfo']['productOrders']
    i = 1
    for productOrders in productOrders_lis:
        # 生成3位随机序号 001
        num = "{:03d}".format(i)
        productOrders['productOrder']['productID'] = str(productID)
        productOrders['productOrder']['productOrderNumber'] = '230A'+str(poOrderNumber)+num
        print('productOrderNumber:', productOrders['productOrder']['productOrderNumber'])
        productID = productID + 1
        i = i+1

    return json_data

def add_note(json_data):
    json_data['orderInfo'].update(orderinfo_add_dict)
    content = {
        "content": json.dumps(json_data, ensure_ascii=False)
    }
    response_head.update(dict(content))
    return response_head


def JKDT_repose(json_data):
    json_data = update_node_value(json_data)
    json_data = add_note(json_data)
    json_data = json.dumps(json_data, ensure_ascii=False, indent=4)
    return json_data


# if __name__ == '__main__':
#     json_data = read_json('json_data.json')
#     json_data = update_node_value(json_data)
#     json_data = add_note(json_data)
#     json_data = json.dumps(json_data, ensure_ascii=False, indent=4)
#     print(json_data)

